package com.yuanlrc.hotel.home;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuanlrc.hotel.news.domain.News;
import com.yuanlrc.hotel.news.service.INewsService;
import com.yuanlrc.system.domain.SysUser;
import com.yuanlrc.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/home/index")
public class HomeNewsController {
    @Autowired
    private INewsService newsService;

    @Autowired
    private ISysUserService sysUserService;
    /*
     * 新闻列表
     */
    @RequestMapping("/news")
    public String news(Model model,
                       @RequestParam(defaultValue = "1") int pageNum){
        PageHelper.startPage(pageNum,4);
        List<News> news = newsService.selectNewsList(new News());
        PageInfo pageInfo = new PageInfo(news);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("news",news);
        return "home/index/news";
    }
    /*
     * 新闻详情页
     */
    @RequestMapping("/news_detail")
    public String news_detail(Model model,Long id){
        if (id==null||id==0){
            return "error/404";
        }
        News news = newsService.selectNewsById(id);
        SysUser sysUser = sysUserService.selectUserById(news.getAuthor());
        news.setUser(sysUser);
        news.setAuthor(sysUser.getUserId());
        news.setHits(news.getHits()+1);
        newsService.updateNews(news);
        model.addAttribute("news",news);
        return "home/index/newsdetail";
    }
}
